package com.easyad.manage;

import java.sql.Timestamp;
import java.util.Calendar;

import com.easyad.hibernate.SdkLog;
import com.easyad.hibernate.SdkFee;
import com.easyad.pub.hibernateDatabaseClass;

public class sdkManage {
	
	public static Calendar INIT_TIME = null;
	
	public static void saveLog(Integer adsiteid, Short adtype, String devicemodel, 
			Short devicetype, Integer lastadindex, Double lat, Double lng, 
			Integer medid, Short nettype, String osversion, String sdkversion,
			Short telcom, String udid, Integer adid, String imei, String ip, Integer status) {
		
		SdkLog log = new SdkLog();
		
		log.setAdsiteid(adsiteid);
		log.setAdid(adid);
		log.setAdtype(adtype);
		log.setDevicemodel(devicemodel);
		log.setDevicetype(devicetype);
		log.setLastadindex(lastadindex);
		log.setLat(lat);
		log.setLng(lng);
		log.setMedid(medid);
		log.setNettype(nettype);
		log.setOsversion(osversion);
		log.setReqtime(new Timestamp(System.currentTimeMillis()));
		log.setSdkversion(sdkversion);
		log.setTelcom(telcom);
		log.setUdid(udid);
		log.setImei(imei);
		log.setIp(ip);
		log.setStatus(status);
		
		hibernateDatabaseClass.save(log);		
	}
	
	public static void saveFee(Short actionflag, Integer adid, Integer adsiteid, String devicemodel, Short feeflag,
			String ip, Double lat, Double lng, Integer medid, Short nettype, String osversion, Timestamp reqtime,
			String sdkversion, Integer status, String udid) {
		
		SdkFee fee = new SdkFee();
		
//		fee.setActionflag(actionflag);
		fee.setAdid(adid);
		fee.setAdsiteid(adsiteid);
//		fee.setDevicemodel(devicemodel);
		fee.setFeeflag(feeflag);
		fee.setIp(ip);
		fee.setLat(lat);
		fee.setLng(lng);
		fee.setMedid(medid);
		fee.setNettype(nettype);
		fee.setOsversion(osversion);
		fee.setReqtime(reqtime);
		fee.setSdkversion(sdkversion);
	//	fee.setStatus(status);
		fee.setUdid(udid);
		
		hibernateDatabaseClass.save(fee);		
	}
	
	public static boolean isNewDay (Calendar newTime){
		
		long diff = newTime.getTimeInMillis() - 
					INIT_TIME.getTimeInMillis();
		return diff > 1000*60*60*24; //one day
		
	}
	
	/**
	 * set INIT_TIME as new day start with 0h0m0s
	 * @param newTime
	 */
	public static void setInitTime(Calendar newTime){
		INIT_TIME =  newTime;
		INIT_TIME.set( 
				 INIT_TIME.get(Calendar.YEAR),
				 INIT_TIME.get(Calendar.MONTH),
				 INIT_TIME.get(Calendar.DAY_OF_MONTH),
				 0,0,0);
	}
}
